/**
 * 
 */
package com.zaprit.common.service.impl;

import java.util.List;

import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import com.rdo.common.bo.Attachment;
import com.rdo.scope.transaction.ORMTransactionManager;
import com.rdo.util.CallBack.CallBack;
import com.zaprit.common.dao.CommonDao;
import com.zaprit.common.exception.CommonException;
import com.zaprit.common.service.CommonService;

/**
 * @author Dell
 */
public class CommonServiceImpl extends ORMTransactionManager implements CommonService
{
	private CommonDao	commonDao	= null;

	/**
	 * @param commonDao
	 *            the commonDao to set
	 */
	public void setCommonDao(CommonDao commonDao)
	{
		this.commonDao = commonDao;
	}

	private <E> E doInTransaction(CallBack<E> callBack, boolean readonly) throws CommonException
	{
		E object = null;
		DefaultTransactionDefinition defaultTransactionDefinition = new DefaultTransactionDefinition();
		TransactionStatus transactionStatus = null;
		try
		{
			transactionStatus = getTransactionManager().getTransaction(defaultTransactionDefinition);
			if (readonly)
			{
				defaultTransactionDefinition.setReadOnly(true);
			}
			object = callBack.execute();
			getTransactionManager().commit(transactionStatus);
		}
		catch (RuntimeException e)
		{
			getTransactionManager().rollback(transactionStatus);
			throw new CommonException(e);
		}
		catch (Exception e)
		{
			getTransactionManager().rollback(transactionStatus);
			throw new CommonException(e);
		}
		return object;
	}

	public Attachment addAttachment(final String scopeId, final String userId, final Attachment attachment)
			throws CommonException
	{
		return doInTransaction(new CallBack<Attachment>()
		{
			public Attachment execute() throws Exception
			{
				return commonDao.addAttachment(userId, attachment);
			}
		}, false);
	}

	public boolean updateAttachment(final String scopeId, final String userId, final Attachment attachment)
			throws CommonException
	{
		return doInTransaction(new CallBack<Boolean>()
		{
			public Boolean execute() throws Exception
			{
				return commonDao.updateAttachment(userId, attachment);
			}
		}, false);
	}

	public Attachment getAttachment(final String scopeId, final String userId, final String attachmentId)
			throws CommonException
	{
		return doInTransaction(new CallBack<Attachment>()
		{
			public Attachment execute() throws Exception
			{
				return commonDao.getAttachment(userId, attachmentId);
			}
		}, false);
	}

	public List<Attachment> getAttachments(final String scopeId, final String userId, final List<String> attachmentIds)
			throws CommonException
	{
		return doInTransaction(new CallBack<List<Attachment>>()
		{
			public List<Attachment> execute() throws Exception
			{
				return commonDao.getAttachments(userId, attachmentIds);
			}
		}, false);
	}
}
